package com.bookmall.cart.web;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.bookmall.cart.config.AlipayTemplate;
import com.bookmall.cart.domain.OrderInfo;
import com.bookmall.cart.domain.vo.*;
import com.bookmall.cart.service.IOrderInfoService;
import com.bookmall.cart.service.IOrderPaymentInfoService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IdUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-21-15:35
 */
@Controller
public class OrderController {
    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private IOrderPaymentInfoService orderPaymentInfoService;

    @Autowired
    private AlipayTemplate alipayTemplate;

    /**
     * 前往支付信息页面
     * @author 胡菜鸡
     * @date 2022/4/24 18:27
     */
    @GetMapping("/pay.html")
    public String pay(Model model, String msg) throws ExecutionException, InterruptedException {
        OrderInfoVo orderInfoVo = orderInfoService.getOrder();

        model.addAttribute("orderInfo", orderInfoVo);
        if(!StringUtils.isBlank(msg)) model.addAttribute("msg", msg);
        return "pay";
    }

    /**
     * 添加一个收货地址
     * @author 胡菜鸡
     * @date 2022/4/22 15:36
     */
    @PostMapping("/orderWeb/addAddress")
    public String addAddress(MemberAddressVo memberAddressVo){
        orderPaymentInfoService.addNewAddress(memberAddressVo);
        return "redirect:http://huyulu666.top/pay.html";
    }

    /**
     * 根据选择的收货地址计算运费
     * @author 胡菜鸡
     * @date 2022/4/24 15:17
     */
    @GetMapping("/orderWeb/getFare")
    @ResponseBody
    public String getFare(@RequestParam("addressId") String addressId){
        BigDecimal fare = orderPaymentInfoService.getFare(addressId);
        return JSON.toJSONString(fare);
    }

    /**
     * 提交订单
     * @author 胡菜鸡
     * @date 2022/4/24 21:08
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo, Model model, RedirectAttributes redirectAttributes){
        //下单操作 -> 下单成功前往支付页，失败回到支付页重新确认支付信息
        System.out.println("订单提交的数据：" + orderSubmitVo);
        try {
            OrderResponseVo result = orderInfoService.submitOrder(orderSubmitVo);
            //下单成功前往支付页
            model.addAttribute("orderResponse", result);
            return "submit";
        } catch (ServiceException e) {
            redirectAttributes.addAttribute("msg", e.getMessage());
            return "redirect:http://huyulu666.top/pay.html";
        }
    }

    @GetMapping("/submit.html")
    public String submit(){
        return "submit";
    }

    /**
     * 获取支付信息并跳转到支付宝支付页面
     * @author 胡菜鸡
     * @date 2022/4/26 17:50
     */
    @ResponseBody
    @GetMapping(value = "/payOrder", produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        PayVo payVo = orderInfoService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);
        return pay;
    }

//    @GetMapping("/test/createOrder")
//    @ResponseBody
//    public String createOrderTest(){
//        OrderInfo orderInfo = new OrderInfo();
//        orderInfo.setOrderSn(IdUtils.simpleUUID());
//        orderInfo.setReceiverName("哈哈哈");
//        orderInfo.setCreateTime(DateUtils.getNowDate());
//        rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", orderInfo);
//        return "ok";
//    }
}
